// SKY BALL SHADER DEFINE.

struct VertexInput {
    @location(0) position: vec3f,
    @location(1) normal: vec3f,
};

struct VertexOutput {
    @builtin(position) clip_position: vec4f,
    @location(0) world_pos: vec3f,
    @location(1) normal: vec3f,
};

// 变换矩阵 - 绑定组0
@group(0) @binding(0) var<uniform> model: mat4x4f;
@group(0) @binding(1) var<uniform> view: mat4x4f;
@group(0) @binding(2) var<uniform> proj: mat4x4f;

// 背景颜色 - 绑定组1
@group(1) @binding(0) var<uniform> background_color: vec4f;

@vertex
fn vs_main(
    @location(0) position: vec3f,
    @location(1) normal: vec3f,
) -> VertexOutput {
    var out: VertexOutput;
    
    // 应用模型-视图-投影变换
    let world_pos = (model * vec4f(position, 1.0)).xyz;
    out.world_pos = world_pos;
    out.normal = normalize((model * vec4f(normal, 0.0)).xyz);
    out.clip_position = proj * view * vec4f(world_pos, 1.0);
    
    return out;
}

// 天空球片元着色器 - 使用Viewport的背景颜色
@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4f {
    // 基于法线方向的渐变效果，使用Viewport的背景颜色
    let base_color = background_color.rgb;
    
    // 基于法线Y分量的渐变效果（从地平线到天空）
    let horizon_factor = max(0.0, dot(in.normal, vec3f(0.0, 1.0, 0.0)));
    
    // 创建从地平线（较亮）到天空（较暗）的渐变
    let horizon_brightness = 1.2;
    let sky_brightness = 0.8;
    let brightness_factor = mix(sky_brightness, horizon_brightness, horizon_factor);
    
    // 应用渐变效果到基础颜色
    let final_color = base_color * brightness_factor;
    
    return vec4f(final_color, background_color.a);
}